<?php
/**
 * @author Евгений Чашин
 *
 */
class Zen_Client {
	/**
	 * Возвращает массив с параметрами пользователя
	 * @param $id int
	 * @return array
	 */
	static function getOne($id) {
		if (! Zend_Registry::isRegistered ( 'client_' . $id )) {
			$model = new Zen_List ( 'client' );
			$table = $model->getTable ();
			$res = $table->find ( $id )->current ();
			if ($res) {
				$res = $res->toArray ();
				Zend_Registry::set ( 'client_' . $id, $res );
			} else {
				return FALSE;
			}
		} else {
			$res = Zend_Registry::get ( 'client_' . $id );
		}
		return $res;
	}
	/**
	 * Возвращает id текущего пользователя
	 * @return int
	 */
	static function getId() {
		$session = new Zend_Session_Namespace ( 'Zend_Auth' );
		$cuser = $session->storage;
		if (! isset ( $cuser->id )) {
			return FALSE;
		} else
			return $cuser->id;
	}
	
	static function getCurrent() {
		$session = new Zend_Session_Namespace ( 'Zend_Auth' );
		$cuser = $session->storage;
		if (! isset ( $cuser->id )) {
			return FALSE;
		} else
			return $cuser;
	}
	
	/**
	 * Возвращает true если пользователь авторизовался false если нет
	 * @return bool
	 */
	static function isLoged() {
		if (Zen_Client::getId ()) {
			return true;
		} else
			return false;
	}
	
	static function hashpass($pass) {
		if ($pass != "") {
			$salt = 'jt5il4jyo5ity45';
			$pepper = 'alw390fy874fy34i9f934';
			return md5 ( $salt . $pass . $pepper );
		} else {
			return false;
		}
	}
	/**
	 * Авторизация пользователя
	 * @param $login string
	 * @param $password string
	 * @param $rem bool Запомнить на сайте
	 * @hashpass $rem bool Зашифрованный пароль или нет
	 * @return bool
	 */
	static function login($login, $password, $rem = false, $hashpass = true) {
		if ($hashpass) {
			$password = Zen_Client::hashpass ( $password );
		}
		$dbAdapter = Zend_Db_Table::getDefaultAdapter ();
		$authAdapter = new Zend_Auth_Adapter_DbTable ( $dbAdapter );
		$authAdapter->setTableName ( 'client' );
		$authAdapter->setIdentityColumn ( 'email' );
		$authAdapter->setCredentialColumn ( 'pass' );
		$authAdapter->setIdentity ( $login );
		$authAdapter->setCredential ( $password );
		$auth = Zend_Auth::getInstance ();
		$result = $auth->authenticate ( $authAdapter );
		if ($result->isValid ()) {
			$data = $authAdapter->getResultRowObject ( null, 'pass' );
			$auth->getStorage ()->write ( $data );
			if ($rem == true) {
				Zend_Session::rememberMe ();
			}
			return $data;
		} else {
			return false;
		
		}
	}
	/**
	 * 
	 * Выход с сайта
	 * 
	 */
	static function logout() {
		Zend_Session::forgetMe ();
		Zend_Auth::getInstance ()->clearIdentity ();
	}
	
	/**
	 * Отправка сообщения с паролем
	 * @param string $email
	 * @return string|string
	 */
	
	static function getip() {
		if (getenv ( "HTTP_CLIENT_IP" ) && strcasecmp ( getenv ( "HTTP_CLIENT_IP" ), "unknown" ))
			$ip = getenv ( "HTTP_CLIENT_IP" );
		elseif (getenv ( "HTTP_X_FORWARDED_FOR" ) && strcasecmp ( getenv ( "HTTP_X_FORWARDED_FOR" ), "unknown" ))
			$ip = getenv ( "HTTP_X_FORWARDED_FOR" );
		elseif (getenv ( "REMOTE_ADDR" ) && strcasecmp ( getenv ( "REMOTE_ADDR" ), "unknown" ))
			$ip = getenv ( "REMOTE_ADDR" );
		elseif (! empty ( $_SERVER ['REMOTE_ADDR'] ) && strcasecmp ( $_SERVER ['REMOTE_ADDR'], "unknown" ))
			$ip = $_SERVER ['REMOTE_ADDR'];
		else
			$ip = "unknown";
		
		return ($ip);
	}
	
	static function rem($user_id) {
		$translate = Zend_Registry::get ( 'Zend_Translate' );
		
		$cnf = Zend_Registry::get ( 'cnf' );
		$user = Zen_Client::getOne ( $user_id );
		
		$view = new Zend_View ();
		$view->setBasePath ( ROOT_PATH . "/site/modules/client/views/scripts/templates/" );
		$view->pass = $user ['new_pass'];
		$view->login = $user ['email'];
		$view->ip = self::getip ();
		$view->domen = $cnf->domen;
		$view->hash = $user ['new_pass_hash'];
		$text = $view->render ( 'rem.phtml' );
		$message = array ("text" => $text, "subject" => $cnf->domen . $translate->_ ( '- Password reminder' ), "from" => "noreply@" . $cnf->domen, "to" => $user ['email'] );
		$email = new Communication_Model_Email ();
		$id = $email->create ( $message );
		$email->send_messages ( $id );
		return true;
	}
	
	static function reg($email = false, $pass = false) {
		if ($email && $pass) {
			$translate = Zend_Registry::get ( 'Zend_Translate' );
			
			$cnf = Zend_Registry::get ( 'cnf' );
			
			$view = new Zend_View ();
			$view->setBasePath ( ROOT_PATH . "/site/modules/client/views/scripts/templates/" );
			$view->pass = $pass;
			$view->email = $email;
			$view->login = $email;
			$view->domen = $cnf->domen;
			
			$text = $view->render ( 'registration.phtml' );
			$message = array ("text" => $text, "subject" => $cnf->domen . $translate->_ ( '- Registration' ), "from" => "noreply@" . $cnf->domen, "to" => $email );
			$email = new Communication_Model_Email ();
			$id = $email->create ( $message );
			$email->send_messages ( $id );
			return true;
		} else {
			return false;
		}
	}
}